package cn.itcast.net.server;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileOutputStream;
import java.net.InetAddress;
import java.util.Properties;

public class ServerWindow extends Frame{
	private FileServer s = new FileServer(7878);
	private Label label;
	private Label lbl_ip;
	private Label lbl_address;
	private Label lbl_os;
	
	public ServerWindow(String title){
		super(title);
		label = new Label();
		lbl_ip = new Label();
		lbl_address = new Label();
		lbl_os = new Label();
this.setLayout(new FlowLayout(FlowLayout.LEFT));
		add(label);
		add(lbl_ip);
		add(lbl_address);
		add(lbl_os);
		label.setText("服务器已经启动");
		try{
			InetAddress addr = InetAddress.getLocalHost();
			lbl_ip.setText("本机IP:"+addr.getHostAddress().toString());//获得本机IP
			lbl_address.setText("本机Name:"+addr.getHostName().toString());//获得本机名称 
			FileOutputStream logFile = new FileOutputStream(new File(System.getProperty("user.dir")+"\\server.properties"));
			Properties properties = new Properties();
			properties.setProperty("ip", addr.getHostAddress().toString());
			properties.setProperty("name", addr.getHostName().toString());
			
			Properties props=System.getProperties(); //获得系统属性集  
			String osName = props.getProperty("os.name"); //操作系统名称  
//			String osArch = props.getProperty("os.arch"); //操作系统构架  
			String osVersion = props.getProperty("os.version"); //操作系统版本 
			lbl_os.setText("本机OS:"+osName+" "+osVersion);//获得本机名称 
			properties.setProperty("osName", osName);
			properties.setProperty("osVersion", osVersion);
			properties.store(logFile, "");
			logFile.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		this.addWindowListener(new WindowListener() {
			public void windowOpened(WindowEvent e) {
				new Thread(new Runnable() {	
					public void run() {
						try {
							s.start();
						} catch (Exception e) {
							//e.printStackTrace();
						}
					}
				}).start();
			}
			
			public void windowIconified(WindowEvent e) {
			}
			
			public void windowDeiconified(WindowEvent e) {
			}
			
			public void windowDeactivated(WindowEvent e) {
			}
			
			public void windowClosing(WindowEvent e) {
				 s.quit();
				 System.exit(0);
			}
			
			public void windowClosed(WindowEvent e) {
			}
			
			public void windowActivated(WindowEvent e) {
			}
		});
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ServerWindow window = new ServerWindow("文件上传服务端"); 
		window.setSize(200, 300); 
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize=kit.getScreenSize();
		window.setBounds(screenSize.width/2, screenSize.height/2, 200, 400);
		window.setVisible(true);
		System.out.println();
	}

}
